package businessServlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.Transform;

import controller.Controller;
import dataBase.Business;


@SuppressWarnings("serial")
public class ShowImage extends HttpServlet {
	private static int defaultWidth = 200;
	private static int defaultHeight = 200;
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		System.out.println("show image");
		String imageKey=req.getParameter("imageKey"); 
		String idString=req.getParameter("id"); 
		String bizKey = req.getParameter("bizKey");
		long bizKeyLong = 0;
		if(bizKey!=null){
			try{
				bizKeyLong = Long.parseLong(bizKey);
			}
			catch (Exception e) {
				return;
			}
			Business b = Controller.getInstance().keyToBusiness(bizKeyLong);
			imageKey = b.getUserName(); 
		}
		System.out.println("image key is: "+imageKey);
		if(imageKey!=null)
			printImage(getTheRightImage(imageKey,idString),req.getParameter("width"),req.getParameter("height"),resp.getOutputStream());
		
	}
	
	public static void printImage(byte[] imageBytes, String stringWidth,String stringHeight, ServletOutputStream servletOutputStream)
	{
		//show the image
		if (imageBytes!=null)
		{

			int intWidth = defaultWidth;
			int intHeight = defaultHeight;

			if (stringWidth!=null)
				intWidth = Math.min(Integer.parseInt(stringWidth),ImagesService.MAX_RESIZE_DIMENSIONS);

			if (stringHeight!=null)
				intHeight = Math.min(Integer.parseInt(stringHeight),ImagesService.MAX_RESIZE_DIMENSIONS);

			System.out.println(intWidth + " "+ intHeight);
			Image oldImage = ImagesServiceFactory.makeImage(imageBytes);
			ImagesService isService = ImagesServiceFactory.getImagesService();
			Transform tfrm = ImagesServiceFactory.makeResize(intWidth,intHeight);
			Image newImage = isService.applyTransform(tfrm, oldImage);

			try {
				servletOutputStream.write(newImage.getImageData());
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		else
			System.out.println("no image to show");


		
	}
	
	private static byte[] getTheRightImage(String userName, String idString)
	{
		Business biz = Controller.getInstance().getBiz(userName);
        
        //get the right image
        if (biz==null)
        	return null;
        
        //logo picture
	    if (idString==null)
	    {
	    	if (biz.getLogoPic()==null)
	    		return null;
	    	else
	        	return biz.getLogoPic().getBytes();
	    }
	        
	    //ad picture   
    	try
    	{
    		int id = Integer.parseInt(idString);
    		ArrayList<Blob> bizAds = biz.getAds();
    		
    		if (id>=0 && id<bizAds.size())
    			return bizAds.get(id).getBytes();
    		else
    			return null;
    		
    	} catch(NumberFormatException e) {
    		System.out.println("ad id isn't a number");
    		return null;
    	}
    	 
		
	}
}
